package com.module.bp.view;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GameState;
import com.dalonedrau.jogl.opengl.View;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.VXBorder;
import com.dalonedrau.jogl.opengl.ui.menu.MenuGroup;
import com.dalonedrau.jogl.opengl.ui.menu.MenuItem;
import com.dalonedrau.jogl.opengl.ui.menu.VerticalMenu;
import com.dalonedrau.jogl.opengl.ui.panel.PagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.jogl.opengl.ui.view.LoadingView;
import com.dalonedrau.jogl.util.UtilityMethods;
import com.module.bp.engine.BPController;

/**
 * The title screen view.
 * @author Donald
 */
public final class IntroView implements View {
	/** the title label. */
	private Label		label;
	/** the title screen menu. */
	private MenuGroup	menu;
	/** the panel used to organize menu items. */
	private PagePanel	panel;
	/** flag indicating initialization has been completed. */
	private boolean		runInit	= false;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleKeyEvent(final KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.out.println("esc");
			GameApplet.getInstance().exit();
			break;
		case KeyEvent.VK_UP:
			menu.highlightPrevious();
			break;
		case KeyEvent.VK_DOWN:
			menu.highlightNext();
			break;
		case KeyEvent.VK_ENTER:
			if (menu != null
					&& menu.isVisible()
					&& menu.getHighlighted() != null) {
				menu.getHighlighted().onSelect();
			}/*
			 * else if (scroller != null && scroller.isVisible() &&
			 * scroller.isScrolling()) { scroller.moveTextToTop(); } else if
			 * (menuIntro != null && menuIntro.isVisible() &&
			 * menuIntro.getHighlighted() != null) {
			 * menuIntro.getHighlighted().onSelect(); }
			 */
			break;
		case KeyEvent.VK_TAB:
			if (menu != null) {
				if (e.isShiftDown()) {
					menu.highlightPrevious();
				} else {
					menu.highlightNext();
				}
			}
			break;
		case KeyEvent.VK_BACK_SPACE:
			try {
				// textfield.removeCharacter();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		case KeyEvent.VK_SPACE:
			try {
				// textfield.addCharacter(e.getKeyChar());
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		default:
			if (Character.isLetterOrDigit(e.getKeyChar())) {
				try {
					// textfield.addCharacter(e.getKeyChar());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
			break;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		System.out.println("OVER");
		panel.handleMouseOver(x, y);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMousePress(final MouseEvent e, final float x,
			final float y) {
		System.out.println("CLICK");
		panel.handleMouseClick(e, x, y);
	}
	/**
	 * Initializes the {@link View}.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		initTitle();
		initMenu();
		panel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		panel.addChild(label);
		panel.addChild(menu);
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(
				panel);
		panel.setPosition(pt.x, pt.y, 0.0f);
		panel.setVisible(true);
	}
	/**
	 * Initializes the title image.
	 * @throws Exception if an error occurs
	 */
	private void initTitle() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img = 
			SpriteImageObjectFactory.getInstance().getImageByName("title_logo");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		label = new Label(sb);		
	}
	/**
	 * Initializes the start menu.
	 * @throws Exception if an error occurs
	 */
	private void initMenu() throws Exception {
		System.out.println("init");
		menu = new VerticalMenu();
		menu.setBorder(new VXBorder());
		MenuItem item = UIFactory.getInstance().getStandardMenuItem(
				"START");
		item.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					// load screen should switch to world map
					LoadingView.getInstance().setNextState(GameState.STATE_12WORLD_MAP);
					LoadingView.getInstance().setLoadAction(
							OverworldView.getInstance(), 
							OverworldView.getInstance().getClass().getMethod(
									"loadMap", String.class, String.class), 
							new String[]{"com/module/bp/dat/xml/bp_map_new.xml", 
								"com/module/bp/dat/xml/hex_map.xml"}
					);
					LoadingView.getInstance().setRendered(false);
					// game state goes to load screen
					GameState.getInstance().setCurrentState(GameState.STATE_11LOADING);
					// GameState.getInstance().setCurrentState(State.IN_PLAY);
					BPController.getInstance().startGame();
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		menu.addItem(item);
		MenuItem item3 = UIFactory.getInstance().getStandardMenuItem(
				"EXIT");
		item3.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				GameApplet.getInstance().exit();
			}
		});
		menu.addItem(item3);
		// highlight the 1st index
		menu.highlightIndex(0);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (!runInit) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		panel.prepareForRendering();
	}
}
